package com.csdj.hzx.Controller;

import com.csdj.hzx.Service.UserMesService;
import com.csdj.hzx.pojo.UserMesCriteria;
import com.github.pagehelper.PageInfo;
import com.lww.springcloudapi.pojo.TabHallSetmeal;
import com.lww.springcloudapi.pojo.TabHallUserbill;
import com.lww.springcloudapi.pojo.TabHallUsermessage;
import com.lww.springcloudapi.result.LayuiEntity;
import com.lww.springcloudapi.result.LayuiEntity2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.List;

@RestController
@CrossOrigin
public class UserMesController {

    @Autowired
    UserMesService userMesService;


    @RequestMapping("/hzx")
    public LayuiEntity findAdT( Model model){
        Jedis jedis = new Jedis("localhost");
        LayuiEntity layuiEntity=new LayuiEntity();
        TabHallUsermessage byUseridA = userMesService.findByUseridA(jedis.get("user"));
        System.out.println(byUseridA+".........>>>>");
        if (byUseridA!=null){
            layuiEntity.setCode("200");
            layuiEntity.setData(byUseridA);

        }else {
            layuiEntity.setCode("200");
            layuiEntity.setData(byUseridA);
            layuiEntity.setMsg("查询失败！");
            System.out.println(layuiEntity.getData());
        }
        return layuiEntity;
    }

    @Transactional
    @RequestMapping("/hzx/get")
    public LayuiEntity2<TabHallUserbill> findAdnM(@RequestParam(defaultValue = "1")Integer page,Integer limit){
        //计算条件
        Integer offset = limit;
        Integer start = (page - 1)*offset;
        Jedis jedis = new Jedis("localhost");
        UserMesCriteria cr = new UserMesCriteria();
        cr.setStart(start);
        cr.setOffset(offset);
        cr.setUserid(jedis.get("user"));

        System.out.println(1);
        LayuiEntity2 layuiEntity=new LayuiEntity2();

        List<TabHallUserbill> allCouFeiI = userMesService.findAllCouFeiI(cr);
        int i = userMesService.selectCount(jedis.get("user"));

        layuiEntity.setData(allCouFeiI);
        layuiEntity.setCount(i);
        return layuiEntity;
    }



    /**
     * 查询用户已使用套餐
     * @param
     * @return data参数
     */
    @RequestMapping("/hzx/ge")
    //开启事务
    @Transactional
    public LayuiEntity findTcan(){
        LayuiEntity layuiEntity=new LayuiEntity();
        Jedis jedis = new Jedis("localhost");
        //获取用户套餐余量
        TabHallUsermessage byUseridA = userMesService.findByUseridA(jedis.get("user"));
        System.out.println(byUseridA+"12312");
        //获取套餐内容
        TabHallSetmeal findabsetmeal = userMesService.findabsetmeal(jedis.get("user"));
        System.out.println(findabsetmeal+"46546");
        //创建已使用套餐对象
        TabHallSetmeal sValue=new TabHallSetmeal();

        sValue.setFlow(findabsetmeal.getFlow()-byUseridA.getTabHallUserpackage().getFlow());
        sValue.setMoney(findabsetmeal.getMoney()-byUseridA.getTabHallUserpackage().getMoney());
        sValue.setMessage(findabsetmeal.getMessage()-byUseridA.getTabHallUserpackage().getMessage());
        sValue.setConversationtime(findabsetmeal.getConversationtime()-byUseridA.getTabHallUserpackage().getConversationtime());
        layuiEntity.setCode("200");
        layuiEntity.setData(sValue);

        System.out.println("========================================="+sValue);

        return layuiEntity;
    }

}
